package cn.csc.module.member.controller.admin.area;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.csc.framework.common.pojo.PageResult;
import cn.csc.framework.common.pojo.CommonResult;
import static cn.csc.framework.common.pojo.CommonResult.success;

import cn.csc.framework.excel.core.util.ExcelUtils;

import cn.csc.framework.operatelog.core.annotations.OperateLog;
import static cn.csc.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.csc.module.member.controller.admin.area.vo.*;
import cn.csc.module.member.dal.dataobject.area.AreaDO;
import cn.csc.module.member.convert.area.AreaConvert;
import cn.csc.module.member.service.area.AreaService;

/**
 * Todo 暂时不启用，使用csc-spring-boot-starter-biz-ip
 */
@Api(tags = "管理后台 - 省市区地区信息")
@RestController
@RequestMapping("/member/area")
@Validated
public class AreaController {

    @Resource
    private AreaService areaService;

    @PostMapping("/create")
    @ApiOperation("创建省市区地区信息")
    @PreAuthorize("@ss.hasPermission('member:area:create')")
    public CommonResult<Long> createArea(@Valid @RequestBody AreaCreateReqVO createReqVO) {
        return success(areaService.createArea(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新省市区地区信息")
    @PreAuthorize("@ss.hasPermission('member:area:update')")
    public CommonResult<Boolean> updateArea(@Valid @RequestBody AreaUpdateReqVO updateReqVO) {
        areaService.updateArea(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除省市区地区信息")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('member:area:delete')")
    public CommonResult<Boolean> deleteArea(@RequestParam("id") Long id) {
        areaService.deleteArea(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得省市区地区信息")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('member:area:query')")
    public CommonResult<AreaRespVO> getArea(@RequestParam("id") Long id) {
        AreaDO area = areaService.getArea(id);
        return success(AreaConvert.INSTANCE.convert(area));
    }

    @GetMapping("/list")
    @ApiOperation("获得省市区地区信息列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('member:area:query')")
    public CommonResult<List<AreaRespVO>> getAreaList(@RequestParam("ids") Collection<Long> ids) {
        List<AreaDO> list = areaService.getAreaList(ids);
        return success(AreaConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得省市区地区信息分页")
    @PreAuthorize("@ss.hasPermission('member:area:query')")
    public CommonResult<PageResult<AreaRespVO>> getAreaPage(@Valid AreaPageReqVO pageVO) {
        PageResult<AreaDO> pageResult = areaService.getAreaPage(pageVO);
        return success(AreaConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出省市区地区信息 Excel")
    @PreAuthorize("@ss.hasPermission('member:area:export')")
    @OperateLog(type = EXPORT)
    public void exportAreaExcel(@Valid AreaExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<AreaDO> list = areaService.getAreaList(exportReqVO);
        // 导出 Excel
        List<AreaExcelVO> datas = AreaConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "省市区地区信息.xls", "数据", AreaExcelVO.class, datas);
    }

}
